package com.settlement.system.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.common.annotation.PreventDuplicateSubmit;
import com.settlement.system.common.result.PageResult;
import com.settlement.system.common.result.Result;
import com.settlement.system.listener.easyexcel.SysLazyToolUserListener;
import com.settlement.system.model.entity.SysLazyToolUser;
import com.settlement.system.model.form.SysLazyToolUserForm;
import com.settlement.system.model.query.SysLazyToolUserQuery;
import com.settlement.system.model.vo.SysLazyToolUserImportVo;
import com.settlement.system.service.SysLazyToolUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 懒人秘籍用户信息表 前端控制器
 * </p>
 *
 * @author huangrensen
 * @since 2023-12-29
 */
@RestController
@RequestMapping("/api/v1/lazyToolUser")
@RequiredArgsConstructor
public class SysLazyToolUserController {

    private final SysLazyToolUserService sysLazyToolUserService;

    @GetMapping("/page")
    @Operation(summary = "查询分页", security = {@SecurityRequirement(name = "Authorization")})
    public PageResult<SysLazyToolUser> getPage(@ParameterObject SysLazyToolUserQuery queryParams) {
        Page<SysLazyToolUser> result = sysLazyToolUserService.getPage(queryParams);
        return PageResult.success(result);
    }


    @Operation(summary = "保存懒人秘籍用户信息表", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping
    @PreventDuplicateSubmit
    @PreAuthorize("@ss.hasPerm('sys:lazyToolUser:save')")
    public Result<?> saveSysLazyToolUser(@RequestBody @Valid SysLazyToolUserForm sysLazyToolUserForm) {
        boolean result = sysLazyToolUserService.saveSysLazyToolUser(sysLazyToolUserForm);
        return Result.judge(result);
    }


    @Operation(summary = "注册懒人秘籍用户", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/register")
    @PreventDuplicateSubmit
    public Result<?> registerSysLazyToolUser(@RequestBody @Valid SysLazyToolUserForm sysLazyToolUserForm) {
        boolean result = sysLazyToolUserService.registerSysLazyToolUser(sysLazyToolUserForm);
        return Result.judge(result);
    }

    @Operation(summary = "启用或者禁用懒人秘籍用户", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/changeUserStatus/{userId}")
    @PreventDuplicateSubmit
    public Result<?> changeUserStatus(@PathVariable Long userId) {
        boolean result = sysLazyToolUserService.changeUserStatus(userId);
        return Result.judge(result);
    }

    @Operation(summary = "查看用户密码", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/getPwd/{userId}")
    @PreventDuplicateSubmit
    public Result<?> getPwd(@PathVariable Long userId) {
        return Result.success(sysLazyToolUserService.getPwd(userId));
    }


    @Operation(summary = "获取懒人秘籍用户信息表信息", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/{id}/form")
    public Result<SysLazyToolUserForm> getSysLazyToolUserFormData(@Parameter(description = "主键ID") @PathVariable Long id) {
        SysLazyToolUserForm sysLazyToolUser = sysLazyToolUserService.getSysLazyToolUserFormData(id);
        return Result.success(sysLazyToolUser);
    }

    @Operation(summary = "把普通用户变成测试用户", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/changeStatus/{id}")
    public Result<?> changeStatus(@Parameter(description = "主键ID") @PathVariable Long id) {
        sysLazyToolUserService.changeStatus(id);
        return Result.success();
    }

    @Operation(summary = "删除懒人秘籍用户信息表", security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:lazyToolUser:delete')")
    public Result<?> deleteSysLazyToolUser(@Parameter(description = "ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = sysLazyToolUserService.deleteSysLazyToolUser(ids);
        return Result.judge(result);
    }

    @Operation(summary = "懒人秘籍用户信息表导入", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/sysLazyToolUserImport")
    @PreAuthorize("@ss.hasPerm('sys:lazyToolUser:import')")
    @PreventDuplicateSubmit
    public Result<?> sysLazyToolUserImport(@RequestParam("file") MultipartFile file) throws Exception {
        SysLazyToolUserListener listener = new SysLazyToolUserListener();
        EasyExcelFactory.read(file.getInputStream(), SysLazyToolUserImportVo.class, listener).sheet(0).doRead();
        List<SysLazyToolUserImportVo> list = listener.getVos();
        if (list == null || list.size() == 0) {
            return Result.success();
        }
        sysLazyToolUserService.importData(list);
        return Result.success();
    }

    @Operation(summary = "导出懒人秘籍用户信息表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/_export")
    @PreAuthorize("@ss.hasPerm('sys:lazyToolUser:export')")
    @PreventDuplicateSubmit
    public void export(@RequestParam("ids") String ids, HttpServletResponse response) throws IOException {
        String fileName = "懒人秘籍用户信息表.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));

        List<SysLazyToolUserImportVo> sysLazyToolUserList = sysLazyToolUserService.findByIds(ids);
        EasyExcel.write(response.getOutputStream(), SysLazyToolUserImportVo.class).sheet("懒人秘籍用户信息表").doWrite(sysLazyToolUserList);
    }
}

